package com.unitechs.trap.write.webapi.service.impl;


import com.unitechs.trap.common.exception.UnitechsServiceException;
import com.unitechs.trap.common.pojo.trap.entity.CountData;
import com.unitechs.trap.common.pojo.trap.entity.CountDataNumber;
import com.unitechs.trap.common.restful.JsonResult;
import com.unitechs.trap.common.restful.ResponseCode;
import com.unitechs.trap.write.service.*;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;

@DubboService
@Service
@Slf4j

public class AllDataServiceImpl implements IAllDataService {
    @DubboReference(timeout = 600000)
    private com.unitechs.trap.excel.service.IAllDataService allDataService;


    @Autowired
    private IAlarmTypeService alarmTypeService;
    @Autowired
    private ITrapTypeService trapTypeService;
    @Autowired
    private ITrapVarBindService trapVarBindService;
    @Autowired
    private ITrapVarNameService trapVarNameService;
    @Autowired
    private ITrapVarValueService trapVarValueService;


    @GlobalTransactional(timeoutMills = 1 * 1024 * 1024 * 1024)
    private CountData action(CountDataNumber countDataNumber, CountData countData, String url, String coName, String version) {

        allDataService.insertAllData(url, coName, version);
        allDataService.insertVarData(url, coName, version);
        countDataNumber.setAlarmtypeDataCount(alarmTypeService.insertAlarmTypes() + countDataNumber.getAlarmtypeDataCount());
        countDataNumber.setTraptypeDataCount(trapTypeService.insertTrapTypes() + countDataNumber.getTraptypeDataCount());
        countDataNumber.setTrapvarbindCount(trapVarBindService.insertTrapVarBinds() + countDataNumber.getTrapvarbindCount());
        countDataNumber.setTrapvarnameDataCount(trapVarNameService.insertTrapVarNames() + countDataNumber.getTrapvarnameDataCount());
        countDataNumber.setTrapvarvalueDataCount(trapVarValueService.insertTrapVarValues() + countDataNumber.getTrapvarvalueDataCount());
        //countDataNumber.setAllData(allDataService.countAllData(url) + countDataNumber.getAllData());


        countData.setAlarmtypeDataCount(countDataNumber.getAlarmtypeDataCount());
        countData.setTraptypeDataCount(countDataNumber.getTraptypeDataCount());
        countData.setTrapvarbindCount(countDataNumber.getTrapvarbindCount());
        countData.setTrapvarnameDataCount(countDataNumber.getTrapvarnameDataCount());
        countData.setTrapvarvalueDataCount(countDataNumber.getTrapvarvalueDataCount());
        //countData.setAllDataCount(countDataNumber.getAllData());
        allDataService.clearVarData();
        allDataService.clearAllData();
        return countData;

    }

    @Override
    public CountData start(String url, String coName, String version) {
        CountData countData = new CountData();
        CountDataNumber countDataNumber = new CountDataNumber();

        countData = action(countDataNumber, countData, url, coName, version);

        return countData;
    }


}
